package com.xuesaieducation.zshddemo;

import android.app.Activity;
import android.os.Handler;

import com.gensee.entity.PingEntity;
import com.gensee.entity.UserInfo;
import com.gensee.net.AbsRtAction;
import com.gensee.player.OnPlayListener;

import java.util.List;

/**
 * Created by yutf on 2018/1/12 0012.
 */

public abstract class GenseeOnPlayCallback implements OnPlayListener
{
    private Activity mContext;
    private Handler mHandler;

    public GenseeOnPlayCallback(Activity mContext)
    {
        this.mContext = mContext;
        mHandler = new Handler(mContext.getMainLooper());
    }

    @Override
    public void onJoin(final int result)
    {
        String msg = "";
        if(result == JOIN_OK)
        {
            mHandler.post(new Runnable()
            {
                @Override
                public void run()
                {
                    onJoinSuccess();
                }
            });
        }else
        {
            switch (result)
            {
                case JOIN_CONNECT_FAILED:
                case JOIN_CONNECT_TIMEOUT:
                case JOIN_RTMP_FAILED:
                    msg = "请检查您的网络链接";
                    break;
                case JOIN_TOO_EARLY:
                    msg = "直播还未开始";
                    break;
                case JOIN_ERROR_REJOIN:
                    msg = "重复加入";
                    break;
                case JOIN_LICENSE:
                    msg = "人数已满";
                    break;
                default:
                    msg = "加入错误" + result;
                    break;
            }
            final String finalMsg = msg;
            mHandler.post(new Runnable()
            {
                @Override
                public void run()
                {
                    onError(result, finalMsg);
                }
            });
        }
    }

    public abstract void onJoinSuccess();

    public abstract void onError(int code, String msg);

    public abstract void onWait(boolean yn);

    public void onRetrieveVideoStart(){}

    public void onRetrievePersonCount(int count){}

    public void OnRetrieveIdc(List<PingEntity> list){}

    public void onRetrieveTitle(String title){}

    public void onRetrieveMicState(int state){}

    public void onRetrieveRespond(int type,boolean isOpen){}

    @Override
    public void onUserJoin(UserInfo userInfo)
    {

    }

    @Override
    public void onUserLeave(UserInfo userInfo)
    {

    }

    @Override
    public void onUserUpdate(UserInfo userInfo)
    {

    }

    @Override
    public void onRosterTotal(final int i)
    {
        mHandler.post(new Runnable()
        {
            @Override
            public void run()
            {
                onRetrievePersonCount(i);
            }
        });
    }

    @Override
    public void onReconnecting()
    {

    }

    @Override
    public void onLeave(int i)
    {

    }

    @Override
    public void onCaching(final boolean b)
    {
        mHandler.post(new Runnable()
        {
            @Override
            public void run()
            {
                onWait(b);
            }
        });
    }

    @Override
    public void onErr(final int errCode)
    {
        String msg = "";
        switch (errCode)
        {
            case AbsRtAction.ErrCode.ERR_DOMAIN:
                msg = "域名domain不正确";
                break;
            case AbsRtAction.ErrCode.ERR_TIME_OUT:
                msg = "请求超时，稍后重试";
                break;
            case AbsRtAction.ErrCode.ERR_SITE_UNUSED:
                msg = "站点不可用，请联系客服或相关人员";
                break;
            case AbsRtAction.ErrCode.ERR_UN_NET:
                msg = "网络不可用，请检查网络连接正常后再试";
                break;
            case AbsRtAction.ErrCode.ERR_SERVICE:
                msg = "service  错误，请确认是webcast还是training";
                break;
            case AbsRtAction.ErrCode.ERR_PARAM:
                msg = "initparam参数不全";
                break;
            case AbsRtAction.ErrCode.ERR_THIRD_CERTIFICATION_AUTHORITY:
                msg = "第三方认证失败";
                break;
            case AbsRtAction.ErrCode.ERR_NUMBER_UNEXIST:
                msg = "编号不存在";
                break;
            case AbsRtAction.ErrCode.ERR_TOKEN:
                msg = "口令错误";
                break;
            case AbsRtAction.ErrCode.ERR_LOGIN:
                msg = "站点登录帐号或登录密码错误";
                break;
            default:
                msg = "错误：errCode = " + errCode;
                break;
        }
        final String finalMsg = msg;
        mHandler.post(new Runnable()
        {
            @Override
            public void run()
            {
                onError(errCode, finalMsg);
            }
        });
    }

    @Override
    public void onDocSwitch(int i, String s)
    {

    }

    @Override
    public void onVideoBegin()
    {
        mHandler.post(new Runnable()
        {
            @Override
            public void run()
            {
                onRetrieveVideoStart();
            }
        });

    }

    @Override
    public void onVideoEnd()
    {

    }

    @Override
    public void onVideoSize(int i, int i1, boolean b)
    {

    }

    @Override
    public void onAudioLevel(int i)
    {

    }

    @Override
    public void onPublish(boolean b)
    {

    }

    @Override
    public void onSubject(final String s)
    {
        mHandler.post(new Runnable()
        {
            @Override
            public void run()
            {
                onRetrieveTitle(s);
            }
        });
    }

    @Override
    public void onPageSize(int i, int i1, int i2)
    {

    }

    @Override
    public void onPublicMsg(long l, String s)
    {

    }

    @Override
    public void onLiveText(String s, String s1)
    {

    }

    @Override
    public void onRollcall(int i)
    {

    }

    @Override
    public void onLottery(int i, String s)
    {

    }

    @Override
    public void onFileShare(int i, String s, String s1)
    {

    }

    @Override
    public void onFileShareDl(int i, String s, String s1)
    {

    }

    @Override
    public void onInvite(final int type,final boolean isOpen)
    {
        mHandler.post(new Runnable()
        {
            @Override
            public void run()
            {
                onRetrieveRespond(type, isOpen);
            }
        });
    }

    @Override
    public void onMicNotify(final int state)
    {
        mHandler.post(new Runnable()
        {
            @Override
            public void run()
            {
                onRetrieveMicState(state);
            }
        });
    }

    @Override
    public void onScreenStatus(boolean b)
    {

    }

    @Override
    public void onModuleFocus(int i)
    {

    }

    @Override
    public void onIdcList(List<PingEntity> list)
    {
        OnRetrieveIdc(list);
        mHandler.post(new Runnable()
        {
            @Override
            public void run()
            {
                onWait(true);
            }
        });
    }
}
